package com.qs.controller;

import com.qs.controller.dto.FormFieldDTO;
import com.qs.controller.vo.FormVO;
import com.qs.mapper.entity.Form;
import com.qs.service.FormService;
import com.qs.service.bo.FormBO;
import com.qs.service.bo.FormFieldBO;
import com.qs.util.BeanCopyUtils;
import com.qs.util.R;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/form")
@RequiredArgsConstructor
public class FormController {
    private final FormService formService;

    @PostMapping
    public R<String> add(@RequestBody FormVO formVO) {
        FormBO formBO = BeanCopyUtils.copyBean(formVO, FormBO.class);
        String result = formService.add(formBO);
        return R.ok(result);
    }

    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable("id") Long id) {
        String result = formService.delete(id);
        return R.ok(result);
    }

    @PutMapping
    public R<String> edit(@RequestBody FormVO formVO) {
        FormBO formBO = BeanCopyUtils.copyBean(formVO, FormBO.class);
        String result = formService.edit(formBO);
        return R.ok(result);
    }

    @GetMapping
    public List<FormBO> list() {
        List<Form> list = formService.list();
        return BeanCopyUtils.copyBeanList(list, FormBO.class);
    }

    @GetMapping("/get/-by-/name/{name}")
    public R<FormBO> getFormByFormName(@PathVariable("name") String name) {
        FormBO formBO = formService.getFormByName(name);
        return R.ok(formBO);
    }
}
